﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Data;
using MES.DataGateway.OracleDAL;
using MES.DataGateway.SyncEntity;
using MES.DataGateway.Common;
using MES.DataGateway.SyncCommon;
using MES.DataGateway.SyncInterface;
using MES.DataGateway.StandardEntity;
using System.IO;
using System.Diagnostics;
namespace MES.DataGateway.StandardDataSync
{
    public class Bom : SyncInfterface
    {
        private AssistantClass m_Assistant = new AssistantClass();
        private TreeView trvMes = new TreeView();
        private TreeView trvEnterprise = new TreeView();
        private ServiceEntity m_ServiceInfoEntity = null;
        private string m_ProjectID = "";
        private string m_LibID = "";
        private string m_DataFilePath = "";
        private OutInfoEntity m_OutParameterObject = null;
        private List<OutInfoEntity> m_OutParameterObjectList = new List<OutInfoEntity>();
        private string m_AccessoriesPath = "";
        private string m_SyncType = "1";
        private string m_strBomForProduct = "";
        private string m_strBomForProductREV = "";
        private string m_FileName = "";//add:Wangjh 2017-9-15
        private StandardDAL m_DAL;
        private XmlDocument m_myXml;//add:Wangjh 20220901
        private const string m_gzPlant = "604";//工装工具分公司的分厂代码 add:wangjh 20241203
        private string m_ProcessPlant = "";//工艺车间 add:wangjh 20241203

        #region 属性
        public string AccessoriesPath
        {
            set { m_AccessoriesPath = value; }
        }

        public OutInfoEntity OutParameterObject
        {
            get { return m_OutParameterObject; }
        }

        public List<OutInfoEntity> OutParameterObjectList
        {
            get { return m_OutParameterObjectList; }
        }

        public ServiceEntity ServiceInfoEntity
        {
            get { return m_ServiceInfoEntity; }
            set { m_ServiceInfoEntity = value; }
        }

        public string ProjectID
        {
            get { return m_ProjectID; }
            set { m_ProjectID = value; }
        }

        public string LibID
        {
            get { return m_LibID; }
            set { m_LibID = value; }
        }

        public string DataFilePath
        {
            get { return m_DataFilePath; }
            set { m_DataFilePath = value; }
        }
        #endregion

        private DataTable ReadXML(string strPath, ref string strfaworkflow)
        {
            m_FileName = Path.GetFileName(strPath);//add:Wangjh 2017-9-15
            XmlDocument myxml = new XmlDocument();
            myxml.Load(strPath);
            DataTable bomdt = new DataTable();
            bomdt.Columns.Add("BomName");
            bomdt.Columns.Add("BomRevision");
            bomdt.Columns.Add("BomDescription");
            bomdt.Columns.Add("ProductName");
            bomdt.Columns.Add("ProductRevision");
            bomdt.Columns.Add("FaOPNO");
            bomdt.Columns.Add("MProductName");
            bomdt.Columns.Add("MProductRevision");
            bomdt.Columns.Add("MProductDesc");
            bomdt.Columns.Add("MSpecName");
            bomdt.Columns.Add("MSpecRevision");
            bomdt.Columns.Add("MQtyRequired");

            //获取物料代号Yuxj20200527
            bomdt.Columns.Add("MaterialCode");
            
           
            //modify by yuxj 20170622
            string ProductName = string.Empty;
            string strProductName = string.Empty;//原始件号，会带H

            string[] productInfo = m_Assistant.GetProductName(strPath);
            ProductName = productInfo[0];
            strProductName = ProductName;
            m_Assistant.ReplaceProductH(myxml, ref ProductName);

            //

            string ProductRevision = productInfo[1];
            XmlNode stu = myxml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/ly2process_no");
            string BomDescription = stu.InnerText.Trim();
            stu = myxml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/ly2Mprocess_no");
            strfaworkflow = stu.InnerText.Trim();
            stu = myxml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/item_revision_id");
            string BomRevision = stu.InnerText.Trim();
            string BomName = string.Format("{0}-{1}", ProductName, BomDescription);//件号-工艺号
            stu = myxml.SelectSingleNode("/Root/WorkFlow/MEOPs");

            m_strBomForProduct = ProductName;
            m_strBomForProductREV = ProductRevision;
            foreach (XmlNode node in stu.ChildNodes)
            {
                if (node.Name != "MEOP")
                    continue;
                //update:Wangjh 20180503 可能没有ly2Mmeop_no字段
                string FaOPNO = "";
                try
                {
                    FaOPNO = node.SelectSingleNode("ly2Mmeop_no").InnerText.Trim();
                }
                catch (Exception) { }
                //
                XmlNode OPnode = node.SelectSingleNode("Bom");
                if (OPnode.ChildNodes.Count == 0)
                { continue; }
                foreach (XmlNode bomnode in OPnode.ChildNodes)
                {
                    if (bomnode.Name != "SubBom")
                        continue;
                    DataRow dr = bomdt.NewRow();
                    dr["BomName"] = BomName;
                    dr["BomRevision"] = BomRevision;
                    dr["BomDescription"] = BomDescription;
                    dr["ProductName"] = ProductName;
                    dr["ProductRevision"] = ProductRevision;
                    dr["FaOPNO"] = FaOPNO;
                    dr["MProductName"] = bomnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();
                    dr["MProductDesc"] = bomnode.SelectSingleNode("object_name").InnerText.Trim();
                    dr["MProductRevision"] = bomnode.SelectSingleNode("item_revision_id").InnerText.Trim();
                    dr["MQtyRequired"] = bomnode.SelectSingleNode("bl_quantity").InnerText.Trim();
                    //获取物料代号Yuxj20200527
                    dr["MaterialCode"] = bomnode.SelectSingleNode("bomcode").InnerText.Trim();
                    XmlNode copnode = node.SelectSingleNode("ly2isCooperation_operation");

                    //if (copnode == null || copnode.InnerText.Trim() != "Y")
                    //{
                        dr["MSpecName"] = string.Format("{0}-{1}-{2}-{3}", strProductName,
                            node.SelectSingleNode("ly2meop_no").InnerText.Trim(),
                            node.SelectSingleNode("object_name").InnerText.Trim(), node.SelectSingleNode("item_id").InnerText.Trim());
                        dr["MSpecRevision"] = node.SelectSingleNode("item_revision_id").InnerText.Trim();
                    //}
                    bomdt.Rows.Add(dr);
                }
            }
            return bomdt;
        }


        /// <summary> 
        /// 工装工具分公司 BOM信息
        ///add:wangjh 20241203
        /// </summary>
        /// <returns></returns>
        private DataTable ReadXMLForGZ(string strPath, out System.Collections.ArrayList bompspdt)
        {
            #region "BOM表"
            DataTable bomdt = new DataTable();
            bomdt.Columns.Add("BomName");
            bomdt.Columns.Add("BomRevision");
            bomdt.Columns.Add("BomDescription");
            bomdt.Columns.Add("ProductName");
            bomdt.Columns.Add("ProductRevision");
            bomdt.Columns.Add("FaOPNO");
            bomdt.Columns.Add("MProductName");
            bomdt.Columns.Add("MProductRevision");
            bomdt.Columns.Add("MProductDesc");
            bomdt.Columns.Add("MSpecName");
            bomdt.Columns.Add("MSpecRevision");
            bomdt.Columns.Add("MQtyRequired");
            //获取物料代号Yuxj20200527
            bomdt.Columns.Add("MaterialCode");
            #endregion

            //modify by yuxj 20170622
            string ProductName = string.Empty;
            string strProductName = string.Empty;//原始件号，会带H

            string[] productInfo = m_Assistant.GetProductName(strPath);
            ProductName = productInfo[0];
            strProductName = ProductName;
            m_Assistant.ReplaceProductH(m_myXml, ref ProductName);

            //

            string ProductRevision = productInfo[1];
            XmlNode stu = m_myXml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/ly2process_no");
            string BomDescription = stu.InnerText.Trim();
            //stu = m_myXml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/ly2Mprocess_no");
            //strfaworkflow = stu.InnerText.Trim();
            stu = m_myXml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/item_revision_id");
            string BomRevision = stu.InnerText.Trim();
            string BomName = string.Format("{0}-{1}", ProductName, BomDescription);//件号-工艺号
            stu = m_myXml.SelectSingleNode("/Root/BOMs");

            m_strBomForProduct = ProductName;
            m_strBomForProductREV = ProductRevision;
            var arrList = new System.Collections.ArrayList();
            var insert = @"insert into bomproductsubproduct(BomProductSubProductID,ProductName,ProductRev,SubProductName,SubProductRev,RequireQty,RequireQty2,TopProductName,TopProductRev,CreateDate)
                        values(sys_guid(),'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',sysdate)";
            var update = @"update bomproductsubproduct set RequireQty='{0}',RequireQty2='{1}' where BomProductSubProductID='{2}'";
            foreach (XmlNode OPnode in stu.ChildNodes)
            {
                if (OPnode.Name != "Bom")
                    continue;
                if (!OPnode.HasChildNodes)
                    continue;
                #region "父BOM"
                    DataRow pdr = bomdt.NewRow();
                    pdr["BomName"] = BomName;
                    pdr["BomRevision"] = BomRevision;
                    pdr["BomDescription"] = BomDescription;
                    pdr["ProductName"] = ProductName;
                    pdr["ProductRevision"] = ProductRevision;
                    pdr["FaOPNO"] = "123GZ"; //FaOPNO;
                    pdr["MProductName"] = OPnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();
                    pdr["MProductDesc"] = OPnode.SelectSingleNode("object_name").InnerText.Trim();
                    pdr["MProductRevision"] = OPnode.SelectSingleNode("item_revision_id").InnerText.Trim();
                    pdr["MQtyRequired"] = OPnode.SelectSingleNode("bl_quantity").InnerText.Trim();
                    //获取物料代号Yuxj20200527
                    pdr["MaterialCode"] = OPnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();
                    pdr["MSpecName"] = "";//工序名称
                    pdr["MSpecRevision"] = "";//工序版本ID
                    bomdt.Rows.Add(pdr);
                #endregion

                #region "子BOM"
                if (OPnode.ChildNodes.Count == 0)
                { continue; }
                foreach (XmlNode bomnode in OPnode.ChildNodes)
                {
                    if (bomnode.Name != "SubBom")
                        continue;
                    if (!bomnode.HasChildNodes)
                        continue;
                    DataRow dr = bomdt.NewRow();
                    dr["BomName"] = BomName;
                    dr["BomRevision"] = BomRevision;
                    dr["BomDescription"] = BomDescription;
                    dr["ProductName"] = ProductName;
                    dr["ProductRevision"] = ProductRevision;
                    dr["FaOPNO"] = "123GZ"; //FaOPNO;
                    dr["MProductName"] = bomnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();
                    dr["MProductDesc"] = bomnode.SelectSingleNode("object_name").InnerText.Trim();
                    dr["MProductRevision"] = bomnode.SelectSingleNode("item_revision_id").InnerText.Trim();
                    dr["MQtyRequired"] = bomnode.SelectSingleNode("bl_quantity").InnerText.Trim();
                    //获取物料代号Yuxj20200527
                    dr["MaterialCode"] = bomnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();

                    dr["MSpecName"] = "";//工序名称
                    dr["MSpecRevision"] = "";//工序版本ID
                    bomdt.Rows.Add(dr);

                    var pProductName = OPnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();//父产品
                    var pProductRev = OPnode.SelectSingleNode("item_revision_id").InnerText.Trim(); ;//父产品版本
                    var SubProductName = bomnode.SelectSingleNode("ae8_drawingNo").InnerText.Trim();//子产品
                    var SubProductRev = bomnode.SelectSingleNode("item_revision_id").InnerText.Trim(); ;//子产品版本
                    var RequireQty = OPnode.SelectSingleNode("bl_quantity").InnerText.Trim(); ;//需要数量
                    var RequireQty2 = bomnode.SelectSingleNode("bl_quantity").InnerText.Trim(); ;//需要数量2
                    var select = @"select * from bomproductsubproduct where ProductName='{0}' and ProductRev='{1}' and SubProductName='{2}' and SubProductRev='{3}' and TopProductName='{4}' and TopProductRev='{5}'";
                    var dt=m_DAL.Query(string.Format(select, pProductName, pProductRev, SubProductName, SubProductRev, ProductName, ProductRevision));
                    if (dt.Rows.Count == 0)
                    {
                        arrList.Add(string.Format(insert, pProductName, pProductRev, SubProductName, SubProductRev, RequireQty, RequireQty2, ProductName, ProductRevision));
                    }
                    else {
                        arrList.Add(string.Format(update, RequireQty, RequireQty2, dt.Rows[0]["BomProductSubProductID"].ToString()));
                    }
                }
                #endregion
            }
            bompspdt = arrList;
            return bomdt;
        }

        private void CreateBom(DataTable bomdt)
        {
            SyncParameterEntity mainEntity = new SyncParameterEntity();
            List<SyncParameterEntity> productList = new List<SyncParameterEntity>();
            ExecuteAPI exectAPI = new ExecuteAPI();
            StandardDAL dal = new StandardDAL();

            foreach (DataRow dr in bomdt.Rows)
            {
                ProductValidate(dr["MProductName"].ToString(),
                    dr["MProductRevision"].ToString(), dr["MProductDesc"].ToString(), dr["MaterialCode"].ToString());
                #region 填充bom结构信息
                mainEntity = new SyncParameterEntity();
                mainEntity.DataList.Add(new DataEntity("Product", "revisionedObjectField", dr["MProductName"].ToString(), dr["MProductRevision"].ToString()));

                if (!dr.IsNull("MSpecName"))
                {
                    mainEntity.DataList.Add(new DataEntity("Spec", "revisionedObjectField", dr["MSpecName"].ToString(), dr["MSpecRevision"].ToString()));
                }

                mainEntity.DataList.Add(new DataEntity("QtyRequired", "dataField", dr["MQtyRequired"].ToString(), ""));
                mainEntity.DataList.Add(new DataEntity("IssueControl", "dataField", "5", ""));
                #endregion
                productList.Add(mainEntity);

            }
            mainEntity = new SyncParameterEntity();
            string strBomName = bomdt.Rows[0]["BomName"].ToString();
            string strVersion = bomdt.Rows[0]["BomRevision"].ToString();

            #region 填充bom基本信息
            mainEntity.DataList.Add(new DataEntity("Name", "dataField", strBomName, ""));
            mainEntity.DataList.Add(new DataEntity("Revision", "dataField", strVersion, ""));
            mainEntity.DataList.Add(new DataEntity("Description", "dataField", bomdt.Rows[0]["BomDescription"].ToString(), ""));
            mainEntity.DataList.Add(new DataEntity("Workflow", "revisionedObjectField", "WF-" + bomdt.Rows[0]["BomDescription"].ToString(), strVersion));
            mainEntity.DataList.Add(new DataEntity("Product", "revisionedObjectField", bomdt.Rows[0]["ProductName"].ToString(), bomdt.Rows[0]["ProductRevision"].ToString()));
            #endregion


            DataTable bomtmpdt = dal.GetRDOByAllVersion("Bom", "BomName", strBomName);
            OutInfoEntity infoEntity = new OutInfoEntity();
            if (bomtmpdt.Rows.Count > 0)
            {
                if (bomtmpdt.Select(string.Format("BomName='{0}' and BomRevision='{1}'", strBomName, strVersion)).Length == 0)
                {
                    string strOldVersion = bomtmpdt.Select("BomID=Revofrcdid")[0]["BomRevision"].ToString();
                    if (strOldVersion.Trim() == "")
                    {
                        Log.WriteLog(strBomName + "未能找到默认版本，无法创建！", 0, "Bom创建");
                        Log.WriteOrcale("PDMTOMES_Bom" + "@^" + strBomName + "未能找到默认版本，无法创建！" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                        return;
                    }
                    else
                    {
                        exectAPI.CreateWorkflowBomVersion(strBomName, strOldVersion, mainEntity, productList, ref infoEntity);
                        OutParameterObjectList.Add(infoEntity);
                        if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-15
                        {
                            Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strBomName + "Bom版本创建成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          

                            #region 继承继承上一版本bom信息 add zhunan 2021.10.9
                            //add zhunan 2021.10.9
                            string oldBomid = bomtmpdt.Select("BomID=Revofrcdid")[0]["BomID"].ToString();
                            m_DAL.inheritPerVersionBomProductInfo(oldBomid, strBomName, strVersion);
                            #endregion

                        }
                        else if (infoEntity.OutFlag == 2)
                        {
                            Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Bom版本创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                        }
                    }
                }
                else
                {
                    Log.WriteLog(strBomName + "." + strVersion + "已经存在！", 0, "Bom创建");
                    Log.WriteOrcale("PDMTOMES_Bom" + "@^" + strBomName + "." + strVersion + "已经存在！" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          

                }
            }
            else
            {
                exectAPI.CreateWorkflowBom(mainEntity, productList, ref infoEntity);
                OutParameterObjectList.Add(infoEntity);
                if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-15
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strBomName + "Bom创建成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                }
                else if (infoEntity.OutFlag == 2)
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Bom创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                }
            }
            ChangeProductForBom(strBomName, strVersion);
        }

        private void CreateBom(DataTable bomdt, string strfaworkflow)
        {
            SyncParameterEntity mainEntity = new SyncParameterEntity();
            List<SyncParameterEntity> productList = new List<SyncParameterEntity>();
            ExecuteAPI exectAPI = new ExecuteAPI();
            StandardDAL dal = new StandardDAL();

            DataTable opnodt = bomdt.DefaultView.ToTable(true, "FaOPNO");
            foreach (DataRow opdr in opnodt.Rows)
            {
                if (string.IsNullOrEmpty(opdr["FaOPNO"].ToString()))
                    continue;
                DataRow[] drs = bomdt.Select(string.Format("FaOPNO='{0}'", opdr["FaOPNO"]));
                productList.Clear();
                string strBomName = string.Format("{0}-{1}", strfaworkflow, opdr["FaOPNO"]);
                string strVersion = drs[0]["BomRevision"].ToString();
                foreach (DataRow dr in bomdt.Rows)
                {
                    ProductValidate(dr["MProductName"].ToString(),
                        dr["MProductRevision"].ToString(), dr["MProductDesc"].ToString(), dr["MaterialCode"].ToString());
                    #region 填充bom结构信息
                    mainEntity = new SyncParameterEntity();
                    mainEntity.DataList.Add(new DataEntity("Product", "revisionedObjectField", dr["MProductName"].ToString(), dr["MProductRevision"].ToString()));
                    if (!dr.IsNull("MSpecName"))
                    {
                        mainEntity.DataList.Add(new DataEntity("Spec", "revisionedObjectField", dr["MSpecName"].ToString(), dr["MSpecRevision"].ToString()));
                    }
                    mainEntity.DataList.Add(new DataEntity("QtyRequired", "dataField", dr["MQtyRequired"].ToString(), ""));
                    mainEntity.DataList.Add(new DataEntity("IssueControl", "dataField", "5", ""));
                    #endregion
                    productList.Add(mainEntity);
                }
                mainEntity = new SyncParameterEntity();
                #region 填充bom基本信息
                mainEntity.DataList.Add(new DataEntity("Name", "dataField", strBomName, ""));
                mainEntity.DataList.Add(new DataEntity("Revision", "dataField", strVersion, ""));
                mainEntity.DataList.Add(new DataEntity("Description", "dataField", bomdt.Rows[0]["BomDescription"].ToString(), ""));
                mainEntity.DataList.Add(new DataEntity("Workflow", "revisionedObjectField", string.Format("{0}-{1}", drs[0]["BomDescription"].ToString(), opdr["FaOPNO"]), strVersion));
                mainEntity.DataList.Add(new DataEntity("Product", "revisionedObjectField", bomdt.Rows[0]["ProductName"].ToString(), bomdt.Rows[0]["ProductRevision"].ToString()));
                #endregion
                Log.SetConfig();//add:Wangjh 2017-9-15 
                DataTable bomtmpdt = dal.GetRDOByAllVersion("Bom", "BomName", strBomName);
                OutInfoEntity infoEntity = new OutInfoEntity();
                if (bomtmpdt.Rows.Count > 0)
                {
                    if (bomtmpdt.Select(string.Format("BomName='{0}' and BomRevision='{1}'", strBomName, strVersion)).Length == 0)
                    {
                        string strOldVersion = bomtmpdt.Select("BomID=Revofrcdid")[0]["BomRevision"].ToString();
                        exectAPI.CreateWorkflowBomVersion(strBomName, strOldVersion, mainEntity, productList, ref infoEntity);
                        OutParameterObjectList.Add(infoEntity);
                        if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-15
                        {
                            Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strBomName + "Bom创建成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                        }
                        else if (infoEntity.OutFlag == 2)
                        {
                            Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Bom创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                        }
                    }
                    else
                    {
                        Log.WriteLog(strBomName + "." + strVersion + "已经存在！", 0, "Bom创建");
                        Log.WriteOrcale("PDMTOMES_Bom" + "@^" + strBomName + "." + strVersion + "已经存在！" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                    }
                }
                else
                {
                    exectAPI.CreateWorkflowBom(mainEntity, productList, ref infoEntity);
                    OutParameterObjectList.Add(infoEntity);
                    if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-15
                    {
                        Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strBomName + "Bom创建成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                    }
                    else if (infoEntity.OutFlag == 2)
                    {
                        Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Bom创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                    }
                }
            }
        }

        private void ProductValidate(string Name, string Revision, string description, string materialcode)
        {
            ExecuteAPI exectAPI = new ExecuteAPI();
            StandardDAL dal = new StandardDAL();
            string strTmpProuctName = Name.Trim();
            string strTmpVersion = Revision.Trim();

            OutInfoEntity infoEntity = new OutInfoEntity();
            SyncParameterEntity productEntity = new SyncParameterEntity();
            productEntity.DataList = new List<DataEntity>();
            productEntity.DataList.Add(new DataEntity("Name", "dataField", strTmpProuctName, ""));
            productEntity.DataList.Add(new DataEntity("Revision", "dataField", strTmpVersion, ""));
            productEntity.DataList.Add(new DataEntity("Description", "dataField", description, ""));
            productEntity.DataList.Add(new DataEntity("ProductType", "namedObjectField", "零件", ""));

            productEntity.DataList.Add(new DataEntity("MaterialCode", "dataField", materialcode, ""));

            DataTable productdt = dal.GetRDOByAllVersion("Product", "ProductName", strTmpProuctName);

            if (productdt.Rows.Count > 0)
            {
                if (productdt.Select(string.Format("ProductName='{0}' and ProductRevision='{1}'", strTmpProuctName, strTmpVersion)).Length == 0)
                {
                    DataRow[] drs =  productdt.Select("ProductID=Revofrcdid");
                    string strVer="";
                    if(drs.Length >0)
                     strVer = drs[0]["ProductRevision"].ToString();

                    exectAPI.CreateRDOEntityRevision("Product", strTmpProuctName, strVer, productEntity, ref infoEntity);
                    if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-26
                    {
                        Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strTmpProuctName + "Product新版本添加成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                    }
                    else if (infoEntity.OutFlag == 2)
                    {
                        Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Product新版本创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                    }
                }
                else 
                {
                    if (materialcode != "")
                    {
                        DataRow[] _drs = productdt.Select("ProductID=Revofrcdid");
                        string strVer = "";
                        if (_drs.Length > 0)
                            strVer = _drs[0]["ProductRevision"].ToString(); 
                        DataRow drs = productdt.Select(string.Format("ProductName='{0}' and ProductRevision='{1}'", strTmpProuctName, strTmpVersion))[0];
                        if (drs["materialcode"].ToString() == "")
                        {
                            exectAPI.UpdateRDOEntity("Product", strTmpProuctName, strVer, productEntity, ref infoEntity);
                            if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-26
                            {
                                Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strTmpProuctName + "Product materialcode更新成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                            }
                            else if (infoEntity.OutFlag == 2)
                            {
                                Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Product materialcode更新失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                            }
 
                        }
                    
                    }                 
 
                }
            }
            else  //没有零件图号
            {

                exectAPI.CreateRDOEntity("Product", productEntity, ref infoEntity);
                if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-26
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + strTmpProuctName + "Product创建成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                }
                else if (infoEntity.OutFlag == 2)
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Product创建失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库          
                }
            }
        }

        public void Execute()
        {
            Log.SetConfig();//add:Wangjh 2017-9-15 
            m_DAL = new StandardDAL();
            m_Assistant.DataFilePath = m_DataFilePath;

            m_myXml = new XmlDocument();
            m_myXml.Load(m_DataFilePath);
            m_ProcessPlant = m_myXml.SelectSingleNode("/Root/WorkFlow/MEProcesss/MEProcess/ly2plant").InnerText; 

            string strfaworkflow = "";
            DataTable bomdt = new DataTable();
            var bompspdt = new System.Collections.ArrayList();
            if (m_ProcessPlant == m_gzPlant)
            {
                bomdt = ReadXMLForGZ(m_DataFilePath, out bompspdt);
            }
            else
            {
                bomdt = ReadXML(m_DataFilePath, ref strfaworkflow);            
            }

            if (bomdt.Rows.Count > 0)
            {
                CreateBom(bomdt);
                //if (!string.IsNullOrEmpty(strfaworkflow))
                //    CreateBom(bomdt, strfaworkflow);
            }
            //BOM父子零件表BomProductSubProduct
            if (bompspdt.Count>0) {
                StandardDAL dal = new StandardDAL();
                dal.ExecuteSqlTran(bompspdt);
            }

        }

        #region 属性和设置
        public void ShowAttribute()
        {

        }

        public string SyncType
        {
            get { return m_SyncType; }
            set { m_SyncType = value; }
        }
        #endregion


        private void ChangeProductForBom(string bomName, string bomVersion)
        {
            if (m_strBomForProduct.Trim() == "")
            {
                return;
            }
            StandardDAL dal = new StandardDAL();
            ExecuteAPI exectAPI = new ExecuteAPI();
            SyncParameterEntity productEntity = new SyncParameterEntity();

            productEntity.DataList = new List<DataEntity>();

            DataSet tmpSet1 = dal.GetProduct(m_strBomForProduct);
            if (tmpSet1.Tables[0].Rows.Count > 0)
            {
                string strVer = dal.GetProductCurrencyVersion(m_strBomForProduct);

                productEntity.DataList.Add(new DataEntity("Bom", "revisionedObjectField", bomName, bomVersion));
                OutInfoEntity infoEntity = new OutInfoEntity();
                exectAPI.UpdateRDOEntity("Product", m_strBomForProduct, strVer, productEntity, ref infoEntity);
                OutParameterObjectList.Add(infoEntity);
                if (infoEntity.OutFlag == 1)//add:Wangjh 2017-9-15
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Product修改成功" + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库   
                }
                else if (infoEntity.OutFlag == 2)
                {
                    Log.WriteOrcale("PDMTOMES_" + infoEntity.StrServicekey + "@^" + "Product修改失败:" + infoEntity.StrInfo + "@^" + m_FileName + "@^" + Convert.ToString(Guid.NewGuid()) + "@^" + m_strBomForProduct + ":" + m_strBomForProductREV);//add:Wangjh 写入数据库   
                }
            }

        }
       
    }
}
